﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace CNKI.TPI.Web.Admin.BLL
{
    public class SubjectHomeService : BaseService, ISubjectHomeService
    {
        ISubjectHomeDao subjectDao = null;
        public SubjectHomeService(ISubjectHomeDao subjectDao)
        {
            this.subjectDao = this.generator.CreateInterfaceProxyWithTarget<ISubjectHomeDao>(subjectDao, new IInterceptor[] { new LogInterceptor() });

        }
        /// <summary>
        /// 根据专题名称查询专题
        /// </summary>
        /// <param name="name"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC> GetTopicByNamePageSize(string name, int start, int length, out int count)
        {
            IList<SYS_CMS_TOPIC> result = subjectDao.GetTopicByWherePageSize(name, start, length, out count);
            foreach (SYS_CMS_TOPIC item in result)
            {
                item.TopicName = StringHelper.ConvertHighlightFlag(item.TopicName);
            }
            return result;
        }

        /// <summary>
        /// 保存专题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveTopic(SYS_CMS_TOPIC obj)
        {
            bool flage = false;
            try
            {
                if (obj.SYSID == 0)
                {
                    flage = subjectDao.InsertTopic(obj);
                    obj.TopicCode = obj.SYSID.ToString();
                    subjectDao.UpdateTopic(obj);
                }
                else
                {
                    if (string.IsNullOrEmpty(obj.TopicPic))
                    {
                        IList<SYS_CMS_TOPIC>  topicInfo = subjectDao.GetSubject(obj.SYSID.ToString());
                        if (topicInfo != null && topicInfo.Count > 0)
                        {
                            obj.TopicPic = topicInfo[0].TopicPic;
                        }
                    }
                    flage = subjectDao.UpdateTopic(obj);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }
        /// <summary>
        /// 删除专题
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteSubject(string sysidstr)
        {
            bool m= subjectDao.DeleteSubject(sysidstr);
            if (m)
            {
                IList<SYS_CMS_TOPIC> list = subjectDao.GetSubject(sysidstr);
                foreach (SYS_CMS_TOPIC item in list)
                {
                    subjectDao.DelageConfigByTopicCode(item.TopicCode);
                }
            }
            return m;
        }

        /// <summary>
        /// 根据导航名称查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_CLS> GetNavByNamePageSize(string name, int start, int length, out int count)
        {

            IList<SYS_CMS_TOPIC_CLS> result = subjectDao.GetNavByWherePageSize(name, start, length, out count);
            foreach (SYS_CMS_TOPIC_CLS item in result)
            {
                item.CLSName = StringHelper.ConvertHighlightFlag(item.CLSName);
            }
            return result;
        }
        /// <summary>
        /// 删除导航
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteNav(string sysidstr)
        {
            bool m= subjectDao.DeleteNav(sysidstr);
            if (m) //删分类
            {
                IList<SYS_CMS_TOPIC_CLS> list=subjectDao.GetBySysIDStr(sysidstr);
                foreach (SYS_CMS_TOPIC_CLS item in list)
                {
                    subjectDao.DeleteByClsCode(item.CLSCode); //删除分类节点
                    subjectDao.DelCateQueryByClsCode(item.CLSCode);//删除分类条件
                }
            }
            return m;
        }

        public bool DeleteDB(string sysidstr)
        {
            bool m = subjectDao.DeleteDB(sysidstr);

            return m;
        }

        /// <summary>
        /// 保存导航
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveNav(SYS_CMS_TOPIC_CLS obj)
        {
            bool flage = false;
            try
            {
                if (obj.SYSID == 0)
                {
                    flage = subjectDao.InsertNav(obj);
                    obj.CLSCode = obj.SYSID.ToString();
                    subjectDao.UpdateNav(obj);
                }
                else
                {
                    flage = subjectDao.UpdateNav(obj);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }

        public bool SaveDB(SYS_CMS_TOPIC_DBCONFIG obj)
        {
            bool flage = false;
            try
            {
                if (obj.SYSID == 0)
                {
                    flage = subjectDao.InsertDB(obj);
                }
                else
                {
                    flage = subjectDao.UpdateDB(obj);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }
        /// <summary>
        /// 判断导航名称是否重复
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistNav(string name, int sysid)
        {
            bool flage = false;
            try
            {
                IList<SYS_CMS_TOPIC_CLS> result = subjectDao.GetNavByName(name);
                if (result != null && result.Count > 0)
                {
                    if (sysid != 0) //修改时判断排除记录本身
                    {
                        if (sysid != result[0].SYSID)
                        {
                            flage = true;
                        }
                    }
                    else
                    {
                        flage = true;

                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }
        /// <summary>
        /// 判断数据源名称是否重复
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistDB(string name, int sysid)
        {
            bool flage = false;
            try
            {
                IList<SYS_CMS_TOPIC_DBCONFIG> result = subjectDao.GetDBByName(name);
                if (result != null && result.Count > 0)
                {
                    if (sysid != 0) //修改时判断排除记录本身
                    {
                        if (sysid != result[0].SYSID)
                        {
                            flage = true;
                        }
                    }
                    else
                    {
                        flage = true;

                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }
        /// <summary>
        /// 根据数据源名称查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_DBCONFIG> GetDBByNamePageSize(string name, int start, int length, out int count)
        {

            IList<SYS_CMS_TOPIC_DBCONFIG> result = subjectDao.GetDBByWherePageSize(name, start, length, out count);
            foreach (SYS_CMS_TOPIC_DBCONFIG item in result)
            {
                item.Name = StringHelper.ConvertHighlightFlag(item.Name);
            }
            return result;
        }

        public IList<SYS_CMS_TOPIC_DBCONFIG> GetDBBySysId(string sysid)
        {

            IList<SYS_CMS_TOPIC_DBCONFIG> result = subjectDao.GetDBBySysId(sysid);
            return result;
        }
        /// <summary>
        /// 判断专题是否重复
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistSubject(string name, int sysid)
        {
            bool flage = false;
            try
            {
                IList<SYS_CMS_TOPIC> result = subjectDao.GetSubjectByName(name);
                if (result != null && result.Count > 0)
                {
                    if (sysid != 0) //修改时判断排除记录本身
                    {
                        if (result.Where(c=>c.SYSID == sysid).Count() == 0)
                        {
                            flage = true;
                        }
                    }
                    else
                    {
                        flage = true;

                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }



        /// <summary>
        /// 保存分类
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveNavCate(string clscode, int sysid, string nodename, string parentcode, int grade)
        {
            if (sysid == 0)//增加
            {
                //获取下一个编码 
                SYS_CMS_TOPIC_CLS_NODE obj = new SYS_CMS_TOPIC_CLS_NODE();
                string dpcode = subjectDao.GetNextCateCode(clscode, parentcode, grade);
                obj.CLSCode = clscode;
                obj.NodeName = nodename;
                obj.NodeCode = dpcode;
                obj.Grade = grade;
                obj.NodeStatus = 1;
                obj.SortNo = int.Parse(dpcode);
                if (grade != 1) //下级节点，则需更新其父节点的 HASCHILDFLAG
                {
                    obj.SySCode = parentcode + dpcode;
                    subjectDao.UpdateNavCateChild(clscode, parentcode, 1);
                }
                else //一级节点
                {
                    obj.SySCode = dpcode;
                }
                return subjectDao.InsertTopicClsNode(obj);
            }
            else
            {
                return subjectDao.UpdateTopicClsNodeName(sysid, nodename);
            }

        }

        /// <summary>
        /// 根据导航编码获取分类信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_CLS_NODE> GetCateByNavCode(string code)
        {
            IList<SYS_CMS_TOPIC_CLS_NODE> result = subjectDao.GetCateByNavCode(code);
            foreach (SYS_CMS_TOPIC_CLS_NODE item in result)
            {
                item.CLSCode = StringHelper.ConvertHighlightFlag(item.CLSCode);
            }
            return result;
        }
        /// <summary>
        /// 获取子节点
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_CLS_NODE> GetCateChilds(string clscode, int grade, string syscode)
        {
            IList<SYS_CMS_TOPIC_CLS_NODE> result = subjectDao.GetCateChilds(clscode, grade, syscode);
            foreach (SYS_CMS_TOPIC_CLS_NODE item in result)
            {
                item.CLSCode = StringHelper.RemoveHighlightFlag(item.CLSCode);
                item.SySCode = StringHelper.RemoveHighlightFlag(item.SySCode);
            }

            return result;
        }

        /// <summary>
        /// 删除分类节点
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteCLsNode(string syscodestr,string clscode)
        {
            bool m = subjectDao.DeleteCateNode(syscodestr, clscode);
            if (m) //删分类
            {
                subjectDao.DelQueryByClsNodeCode(clscode,syscodestr);
            }
            return m;
        }
        /// <summary>
        /// 保存检索条件
        /// </summary>
        /// <param name="order"></param>
        /// <param name="querlist"></param>
        /// <param name="dbcode"></param>
        /// <param name="dbname"></param>
        /// <param name="nodecode"></param>
        /// <returns></returns>
        public bool SaveQuery(string order, IList<SearchQuery> querlist, string dbcode, string dbname, string nodecode, string clsCode)
        {
            string where = ""; ;
            if (querlist != null)
            {
                foreach (SearchQuery item in querlist)
                {
                    string formatstr = " {0} ({1} {3} '{2}') ";
                    where += string.IsNullOrEmpty(item.Text.Trim()) ? "" : (string.Format(formatstr, item.Type, item.Key, item.Text.Trim(), item.ConditionType));
                }
            }
            if (where != "")
            {
                where = where.Substring(4, where.Length - 4);
            }
            SYS_CMS_TOPIC_CLS_QUERY obj = new SYS_CMS_TOPIC_CLS_QUERY();
            obj.DBCode = dbcode;
            obj.DBName = dbname;
            obj.Query = where;
            obj.Sort = order;
            obj.Status = 1;
            obj.NodeCode = nodecode;
            obj.CLSCode = clsCode;
            IList<SYS_CMS_TOPIC_CLS_QUERY> objquery = subjectDao.GetQueryByDBNode(dbcode, nodecode);
            if (objquery != null && objquery.Count > 0)//已存在则更新
            {
                obj.SYSID = objquery[0].SYSID;
                return subjectDao.UpdateTopicQuery(obj);
            }
            else
            {
                return subjectDao.InsertTopicQuery(obj);
            }
        }

        /// <summary>
        /// 根据数据库和分类code称查询专题
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_CLS_QUERY> GetQueryPageSize(string dbname, string nodecode,string navCode, int start, int length, out int count)
        {
            IList<SYS_CMS_TOPIC_CLS_QUERY> result = subjectDao.GetQueryByPageSize(dbname, nodecode,navCode, start, length, out count);
            foreach (SYS_CMS_TOPIC_CLS_QUERY item in result)
            {
                item.DBName = StringHelper.ConvertHighlightFlag(item.DBName);
            }
            return result;
        }
        /// <summary>
        /// 删除分类条件
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteCLsQuery(string sysidstr)
        {
            return subjectDao.DeleteCateQuery(sysidstr);
        }

        /// <summary>
        /// 根据查询条件获取SYS_CMS_TOPIC_PAGECONFIG数据
        /// </summary>
        /// <param name="match"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_PAGECONFIG> GetPageConfigPageSize(string columname, string topiccode, int start, int length, out int count)
        {
            IList<SYS_CMS_TOPIC_PAGECONFIG> result = subjectDao.GetPageConfigByPageSize(columname, topiccode, start, length, out count);
            foreach (SYS_CMS_TOPIC_PAGECONFIG item in result)
            {
                item.ColumnName = StringHelper.ConvertHighlightFlag(item.ColumnName);
                item.TopicCode = StringHelper.ConvertHighlightFlag(item.TopicCode);
            }

            return result;
        }

        /// <summary>
        /// 获取所有分类数据
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_CLS> GetNavAll()
        {
            return subjectDao.GetNavByName(null);
        }
        public IList<SYS_CMS_TOPIC_CLS_QUERY> GetQueryByNodeCode(string nodecode,string clscode)
        {
            return subjectDao.GetQueryByCLSCode(nodecode,clscode);
        }

        /// <summary>
        /// 获取所有数据源数据
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_TOPIC_DBCONFIG> GetDBAll()
        {
            return subjectDao.GetDBByName(null);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SavePageConfig(SYS_CMS_TOPIC_PAGECONFIG obj)
        {
            bool flage = false;
            try
            {
                if (obj.SYSID == 0)
                {
                    flage = subjectDao.InsertPageConfig(obj);
                }
                else
                {
                    flage = subjectDao.UpdatePageConfig(obj);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return flage;
        }


        public IList<SYS_CMS_TOPIC_PAGECONFIG> GetPageConfigByID(string sysid)
        {
            return subjectDao.GetPageConfig(sysid);
        }
        public bool DeletePageConfig(string sysidstr)
        {
            return subjectDao.DeletePageConfig(sysidstr);
        }
        /// <summary>
        /// 修改显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdatePageSort(IList<SYS_CMS_TOPIC_PAGECONFIG> list)
        {
            return subjectDao.UpdatePageSort(list);
        }
        public IList<SYS_CMS_TOPIC_CLS_NODE> GetByCLSNodeCode(string clscode, string nodecode)
        {
            return subjectDao.GetByCLSNodeCode(clscode,nodecode);
        }
        public IList<SYS_CMS_TOPIC_PAGECONFIG> GetPageConfigAll()
        {
            return subjectDao.GetPageConfigAll();
        }
    }
}
